using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class Aim : MonoBehaviour
	{
		public GameObject balls;

		public Transform[] muzzles;

		public int curShootIndex = 1;

		public Transform ball;

		private Rigidbody2D[] allBall;

		private LineRenderer aimLine;

		public Transform CriticalPointLeft;

		public Transform CriticalPointRight;

		public float shootingSpeed = 20f;

		public GameObject levelPanel;

		private bool levelStop;

		public int ballNum = 50;

		public int addBallNum;

		public Text ballText;

		public GameObject line;

		public LineManager lineMgr;

		public Vector2 shootPosition;

		public bool onceShootOver;

		private void Start()
		{
			Time.timeScale = 1f;
			aimLine = GetComponent<LineRenderer>();
			for (int i = 0; i < ballNum + addBallNum; i++)
			{
				Transform transform = UnityEngine.Object.Instantiate(ball);
				transform.parent = balls.GetComponent<Transform>();
				transform.position = getMuzzlePos();
				transform.gameObject.layer = 9;
			}
			allBall = balls.GetComponentsInChildren<Rigidbody2D>();
			OnShowBalls(ballNum, addBallNum);
		}

		public void OnShowBalls(int n1, int n2)
		{
			if (n2 > 0)
			{
				ballText.text = "x" + n1.ToString() + "+" + n2.ToString();
			}
			else if (n1 > 0)
			{
				ballText.text = "x" + n1.ToString();
			}
			else
			{
				ballText.text = string.Empty;
			}
		}

		public Vector2 getMuzzlePos()
		{
			Transform muzzle = getMuzzle();
			Vector3 position = muzzle.position;
			float x = position.x;
			Vector3 position2 = muzzle.position;
			return new Vector2(x, position2.y);
		}

		public Transform getMuzzle()
		{
			return muzzles[curShootIndex];
		}

		public Transform getNexMuzzle()
		{
			return muzzles[(curShootIndex + 1) % 3];
		}

		private void Update()
		{
			AimLaunch();
			OnCheckReady();
		}

		public bool Homing()
		{
			for (int i = 0; i < allBall.Length; i++)
			{
				if (allBall[i].GetComponent<BallMove>().state != 0)
				{
					return false;
				}
			}
			return true;
		}

		private void OnCheckReady()
		{
			if (!onceShootOver)
			{
				return;
			}
			for (int i = 0; i < allBall.Length; i++)
			{
				if (allBall[i].GetComponent<BallMove>().state == BallState.Battle)
				{
					return;
				}
			}
			onceShootOver = false;
			curShootIndex = (curShootIndex + 1) % 3;
		}

		private bool InSafeRect(Vector2 v)
		{
			if (v.y > 820f || v.y < 130f)
			{
				return false;
			}
			return true;
		}

		private void AimLaunch()
		{
			if (onceShootOver)
			{
				return;
			}
			if (Input.GetMouseButtonDown(0))
			{
				if (!InSafeRect(UnityEngine.Input.mousePosition))
				{
					return;
				}
				Vector2 a = Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);
				Vector2 vector = a - getMuzzlePos();
				lineMgr.Change(vector.normalized, getMuzzlePos());
			}
			if (Input.GetMouseButton(0))
			{
				if (!InSafeRect(UnityEngine.Input.mousePosition))
				{
					return;
				}
				Vector2 a2 = Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);
				Vector2 vector2 = a2 - getMuzzlePos();
				lineMgr.Change(vector2.normalized, getMuzzlePos());
			}
			if (Input.GetMouseButtonUp(0) && InSafeRect(UnityEngine.Input.mousePosition))
			{
				onceShootOver = true;
				allBall = balls.GetComponentsInChildren<Rigidbody2D>();
				shootPosition = Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);
				StartCoroutine(LineLaunch(getMuzzle().position));
				levelStop = !levelStop;
				lineMgr.HideAll();
			}
		}

		private IEnumerator LineLaunch(Vector2 muzzlePos)
		{
			Vector2 pos = shootPosition;
			Vector2 _direction = pos - muzzlePos;
			int curCount = ballNum;
			int curAdd = addBallNum;
			for (int i = 0; i < allBall.Length; i++)
			{
				allBall[i].GetComponent<BallMove>().state = BallState.Battle;
				allBall[i].GetComponent<Rigidbody2D>().AddForce(_direction * shootingSpeed);
				allBall[i].GetComponent<Rigidbody2D>().velocity = _direction.normalized * 1000f;
				yield return new WaitForSeconds(0.1f);
				if (curAdd == 0)
				{
					curCount--;
				}
				else
				{
					curAdd--;
				}
				OnShowBalls(curCount, curAdd);
			}
		}

		private Vector3 DirectionRestriction(Vector3 v, Transform left, Transform right)
		{
			float x = v.x;
			Vector3 position = left.position;
			if (x < position.x)
			{
				Vector3 position2 = left.position;
				v.x = position2.x;
			}
			float x2 = v.x;
			Vector3 position3 = right.position;
			if (x2 > position3.x)
			{
				Vector3 position4 = right.position;
				v.x = position4.x;
			}
			float y = v.y;
			Vector3 position5 = left.position;
			if (y > position5.y)
			{
				Vector3 position6 = left.position;
				v.y = position6.y;
			}
			return v;
		}
	}
}

